#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""所有类的基础类
提供各种工具和方法
"""
from Config import Config
import datetime


class Base:
    def __init__(self):
        self.class_name = self.__class__.__name__
        now = datetime.datetime.now()
        self.now = now.year.__str__() + \
                   now.month.__str__() + \
                   now.day.__str__() + \
                   now.hour.__str__() + \
                   now.minute.__str__() + \
                   now.second.__str__() + \
                   now.microsecond.__str__()
        # 获取配置文件
        ini = Config.instance()
        self.cfg = ini.cfg
        # 获取日志文件
        self.log_name = self.cfg['path']['log_path'] + '/' + self.cfg['log']['log_name'] + '.log'

    def error(self, message):
        self.run_info('[error]' + message, True, '\033[31m')
        exit('[' + self.class_name + ']' + message)

    # 打印调试信息 可以通过配置文件关闭
    def debug(self, message, tag='', is_run=True):
        if tag:
            function_str = '[tag:' + tag + ']'
        else:
            function_str = ''
        message = '[debug][class:' + self.class_name + ']' + function_str + str(message)
        if is_run:
            if self.cfg.getboolean('log', 'has_debug'):
                self.run_info(message, is_run, '\033[34m', False)

    # 打印记录信息
    def run_info(self, message, has_print=True, color='\033[0m', has_log=True):
        time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        msg = time + " " + str(message)
        if has_print:
            print(color + msg + '\033[0m')
        if self.cfg.getboolean('log', 'is_write_file') and has_log:
            with open(self.log_name, 'a+') as f:
                f.write(msg + '\n')

    # 打印配置文件信息
    def write_config(self, has_print=False):
        self.run_info('***执行配置***', has_print)
        for section in self.cfg:
            for option in self.cfg[section]:
                self.run_info('[ ' + section + ' ][ ' + option + ' ] = ' + self.cfg[section][option], has_print)
        self.run_info('***执行配置***', has_print)


if __name__ == "__main__":
    p = Base()
    print(p.cfg.getboolean('question_type', 'get_sentence_by_new_flower_order'))
